package app.auto.runner.base.intf.json;

/**
 * Createdby87716on2017/3/3.
 */

import com.google.gson.Gson;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import app.auto.runner.base.intf.ListBuilder;

public class JsonToMapUtils {
    /** 日志文件生成器 */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> parseJsonToMap(String jsonStr) {
        Map<String, Object> map = new HashMap<String, Object>();
        //最外层解析  
        JSONObject json = JSONObject.fromObject(jsonStr);
        for (Object k : json.keySet()) {
            Object v = json.get(k);
            //如果内层还是数组的话，继续解析
            if (v instanceof JSONArray) {
                List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                Iterator<JSONObject> it = ((JSONArray) v).iterator();
                while (it.hasNext()) {
                    JSONObject json2 = it.next();
                    list.add(parseJsonToMap(json2.toString()));
                }
                map.put(k.toString(), list);
            } else {
                map.put(k.toString(), v);
            }
        }
        return map;
    }

    public static String objectToJson(Object obj) {
        if (obj instanceof Map) {
            return mapToJson((Map<String, String>) obj);
        } else if (obj instanceof List) {
            return listtoJson((List) obj);
        }
        return obj.toString();
    }

    public static String mapToJson(Map<String, String> map) {
        Set<String> keys = map.keySet();
        String key = "";
        Object value = "";
        StringBuffer jsonBuffer = new StringBuffer();
        jsonBuffer.append("{");
        for (Iterator<String> it = keys.iterator(); it.hasNext(); ) {
            key = (String) it.next();
            value = map.get(key);
            String json = "";
            if (value instanceof List) {
                json = listtoJson((List) value);

            } else if (value instanceof Map) {
                json = mapToJson(((Map) value));
            } else {
                json = "\"" + value + "\"";
            }
            jsonBuffer.append("\"" + key + "\"" + ":" + json);
            if (it.hasNext()) {
                jsonBuffer.append(",");
            }
        }
        jsonBuffer.append("}");
        return jsonBuffer.toString().replaceAll("\"\\{", "{").replaceAll("\\}\"", "}");
    }

    public static String listtoJson(List value) {
        ListBuilder listBuilder = ListBuilder.build();

        String json;
        String m = "";
        for (Object o : ((List) value)) {

            if (o instanceof Map || o instanceof List) {
                m = mapToJson((Map) o);
            } else {
                m = value.toString();
            }
            listBuilder.add(m);
        }
        json = listBuilder.get().toString();
        return json;
    }

    private static Gson gson = new Gson();

    public static String entityToJson(Object src) {
        return gson.toJson(src);
    }

    public static List jsontoList(String json) {
        try {
            org.json.JSONArray array = new org.json.JSONArray(json);
            ListBuilder lb = ListBuilder.build();
            for (int i = 0; i < array.length(); i++) {
                lb.add(array.getString(i));
            }
            return lb.get();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return new ArrayList();
    }
}  